package org.example.controller;

import org.apache.ibatis.annotations.Param;
import org.example.domain.NitRecord;
import org.example.service.impl.NitRecordServiceImpl;
import org.example.untils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("nitRecord")
public class NitRecordController {

    @Autowired
    private NitRecordServiceImpl nitRecordServiceImpl;

    @PostMapping("/insertOneNitRecord")
    public Result insertOneNitRecord(@RequestBody NitRecord nitRecord){
        nitRecordServiceImpl.insertOneNitRecord(nitRecord);
        return new Result(200,"反馈成功~",null);
    }

    @GetMapping("/selectNitRecordsByUserId")
    public Result selectNitRecordsByUserId(@Param("userId") Integer userId){
        NitRecord[] nitRecords= nitRecordServiceImpl.selectNitRecordsByUserId(userId);
        return new Result(200,"获取成功~",nitRecords);
    }

    @GetMapping("/selectNitRecordsByAnswer")
    public Result selectNitRecordsByAnswer(@Param("isAnswer") Integer isAnswer,@Param("adminId") Integer adminId){
        NitRecord[] nitRecords = nitRecordServiceImpl.selectNitRecordsByAnswer(isAnswer,adminId);
        return new Result(200,"获取成功",nitRecords);
    }

    @PostMapping("/answerNitRecordById")
    public Result answerNitRecordById(@RequestBody NitRecord nitRecord){
        Integer status = nitRecordServiceImpl.answerNitRecordById(nitRecord);
        if(status==0){
            return new Result(201,"已被答复",null);
        } else {
            return new Result(200,"答复成功",null);
        }
    }

    @GetMapping("/selectContinueNitRecordsByUserId")
    public Result selectContinueNitRecordsByUserId(@Param("userId") Integer userId){
        ArrayList<NitRecord> nitRecords = nitRecordServiceImpl.selectContinueNitRecordsByUserId(userId);
        ArrayList<NitRecord> newNitRecords = new ArrayList<>();
        for (int i = nitRecords.size();i>0;i--){//将获取到的数据反转，最新的放在最上面
            newNitRecords.add(nitRecords.get(i-1));
        }
        return new Result(200,"获取成功",newNitRecords);
    }

    @GetMapping("/selectContinueNitRecordsByAdminId")
    public Result selectContinueNitRecordsByAdminId(@Param("adminId") Integer adminId){
        ArrayList<NitRecord> nitRecords = nitRecordServiceImpl.selectContinueNitRecordsByAdminId(adminId);
        ArrayList<NitRecord> newNitRecords = new ArrayList<>();
        for (int i = nitRecords.size();i>0;i--){//将获取到的数据反转，最新的放在最上面
            newNitRecords.add(nitRecords.get(i-1));
        }
        return new Result(200,"获取成功",newNitRecords);
    }

}
